package com.castle.fortress.admin.member.setting.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.setting.entity.MemberSettingEntity;
import com.castle.fortress.admin.member.setting.dto.MemberSettingDto;
import com.castle.fortress.admin.member.setting.service.MemberSettingService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import java.util.List;

/**
 * 会员设置表 控制器
 *
 * @author 
 * @since 2025-04-15
 */
@Api(tags="会员设置表管理控制器")
@Controller
public class MemberSettingController {
    @Autowired
    private MemberSettingService memberSettingService;

    /**
     * 页面
     * @return
     */
/*    @ApiIgnore
    @RequestMapping({"/page/member/setting/memberSetting"})
    public String toPage(){
        return "admin/member/setting/pages-memberSetting.html";
    }*/

    /**
     * 会员设置表的分页展示
     * @param memberSettingDto 会员设置表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("会员设置表分页展示")
    @GetMapping("/member/setting/memberSetting/page")
    @ResponseBody
    @RequiresPermissions("member:setting:memberSetting:pageList")
    public RespBody<IPage<MemberSettingDto>> pageMemberSetting(MemberSettingDto memberSettingDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<MemberSettingDto> page = new Page(pageIndex, pageSize);
        IPage<MemberSettingDto> pages = memberSettingService.pageMemberSettingExtends(page, memberSettingDto);

        return RespBody.data(pages);
    }

    /**
     * 会员设置表的列表展示
     * @param memberSettingDto 会员设置表实体类
     * @return
     */
    @ApiOperation("会员设置表列表展示")
    @GetMapping("/member/setting/memberSetting/list")
    @ResponseBody
    public RespBody<List<MemberSettingDto>> listMemberSetting(MemberSettingDto memberSettingDto){
        List<MemberSettingDto> list = memberSettingService.listMemberSetting(memberSettingDto);
        return RespBody.data(list);
    }

    /**
     * 会员设置表保存
     * @param memberSettingDto 会员设置表实体类
     * @return
     */
    @ApiOperation("会员设置表保存")
    @PostMapping("/member/setting/memberSetting/save")
    @ResponseBody
    @RequiresPermissions("member:setting:memberSetting:save")
    public RespBody<String> saveMemberSetting(@RequestBody MemberSettingDto memberSettingDto){
        if(memberSettingDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        MemberSettingEntity memberSettingEntity = ConvertUtil.transformObj(memberSettingDto,MemberSettingEntity.class);
        if(memberSettingService.save(memberSettingEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员设置表编辑
     * @param memberSettingDto 会员设置表实体类
     * @return
     */
    @ApiOperation("会员设置表编辑")
    @PostMapping("/member/setting/memberSetting/edit")
    @ResponseBody
    @RequiresPermissions("member:setting:memberSetting:edit")
    public RespBody<String> updateMemberSetting(@RequestBody MemberSettingDto memberSettingDto){
        if(memberSettingDto == null || memberSettingDto.getId() == null || memberSettingDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        MemberSettingEntity memberSettingEntity = ConvertUtil.transformObj(memberSettingDto,MemberSettingEntity.class);
        if(memberSettingService.updateById(memberSettingEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员设置表删除
     * @param id
     * @return
     */
    @ApiOperation("会员设置表删除")
    @PostMapping("/member/setting/memberSetting/delete")
    @ResponseBody
    @RequiresPermissions("member:setting:memberSetting:delete")
    public RespBody<String> deleteMemberSetting(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberSettingService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 会员设置表批量删除
     * @param ids
     * @return
     */
    @ApiOperation("会员设置表批量删除")
    @PostMapping("/member/setting/memberSetting/deleteBatch")
    @ResponseBody
    @RequiresPermissions("member:setting:memberSetting:deleteBatch")
    public RespBody<String> deleteMemberSettingBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberSettingService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员设置表详情
     * @param id 会员设置表id
     * @return
     */
    @ApiOperation("会员设置表详情")
    @GetMapping("/member/setting/memberSetting/info")
    @ResponseBody
    @RequiresPermissions("member:setting:memberSetting:info")
    public RespBody<MemberSettingDto> infoMemberSetting(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        MemberSettingDto memberSettingDto =  memberSettingService.getByIdExtends(id);

        return RespBody.data(memberSettingDto);
    }


}
